package com.yh.web;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yh.pojo.Role;
import com.yh.pojo.UserInfo;
import com.yh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.core.userdetails.User;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author by 张晨光
 * @date 2023/11/6 16:55
 */
@Controller
@RequestMapping("user")
public class UserController {
    //调用业务层bean
    @Autowired
    UserService userService;

    /**
     * 前端页面的  参数设置省略试下：    ?page=1&size=5
     * @param page
     * @param size
     * @param model
     * @return
     */
    @GetMapping("findAll")
    public String findAll(@RequestParam(value = "page",defaultValue = "1") Integer page,
                          @RequestParam(value = "size",defaultValue = "5") Integer size, Model model){
        PageHelper.startPage(page,size);
        List<UserInfo> userInfoList = userService.findAllUsers();
        //设置分页;
        PageInfo<UserInfo>pageInfo=new PageInfo<>(userInfoList);
        model.addAttribute("pageInfo",pageInfo);
        return "user-list";
    }

    @PostMapping("save")
    public String save(UserInfo userInfo){
        userService.save(userInfo);
        return "redirect:findAll";
    }

    /**
     * 根据id，来查询用户详情信息;
     * @param id    ,不加参数可否？
     * @return
     */
    @GetMapping("findById")
    public String  findUserById(Integer id,Model model) {
        UserInfo userInfo = userService.findUserById(id);
        model.addAttribute("user",userInfo);
        return "user-show";
    }

    /**
     * 根据用户的id，来决定添加哪些角色？
     * @param id
     * @return
     */
    @GetMapping("findUserByIdAndAllRole")
    public String findUserByIdAndAllRole(Integer id,Model model){
        UserInfo userInfo = userService.findSimpleUserById(id);
        //去查看页面：user-role-add.html，里面的${user.id}
        model.addAttribute("user",userInfo);
        List<Role> roleList = userService.findOtherRoles(id);
        model.addAttribute("roleList",roleList);
        //去用户-角色 新增页面;
        return "user-role-add";
    }

    /**
     * @RequestParam("userId")和页面上的name="userId"有关系;和后面的Integer userId,无关;
     *
     * @param userId
     * @return
     */
    @PostMapping("addRoleToUser")
    public String  addRoleToUser(@RequestParam("userId") Integer userId,
                                 @RequestParam("ids") Integer ... rds ){
            userService.addRoleToUser(userId,rds);
            return "redirect:findAll";
    }

    @GetMapping("deleteUser")
    public String deleteUser(Integer id){
        userService.deleteUser(id);
        return "redirect:findAll";
    }

    /**
     * 获取用户名;已经存到了安全框架的user对象里面;
     * 转换成json格式;
     */
    @GetMapping("getUsername")
    @ResponseBody
    public String getUserName(){
        //如何来获取该用户名？
        //上下文持有者;获取上下文对象;获取对象的身份认证信息;主要信息; --》返回一个主要的信息认证对象,Object
        User user = (User) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return user.getUsername();
    }
}
